<?php
namespace App\Api\Controllers;

class UploadController{

    /*
     * 上传图片
     */
    public function uploadImage($file)
    {
        if ($file->getClientOriginalExtension() &&
            !in_array(strtolower($file->getClientOriginalExtension()), ['png', 'jpg', 'jpeg', 'gif'])) {
            return [
                'code' => 0,
                'msg' => '上传格式错误'
            ];
        }

        $upload_path = 'uploads/images/'. date('Ym', time());
        if(!file_exists($upload_path)){
            @mkdir($upload_path, 0777);
        }

        $file_name = explode('.', $file->getClientOriginalName());
        $file_name = md5($file_name[count($file_name) - 2] .
                rand(1, time())).'.'. $file_name[count($file_name) -1];
        $result = $file->move($upload_path, $file_name);

        return [
            'code' => 1,
            'msg' => '上传成功',
            'data'=> env('APP_URL').'/uploads/'.ltrim($upload_path. '/' . $result->getFilename(), 'uploads/')
        ];
    }



}